package com.tianci.appstore.ui.util;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppAction;
import com.tianci.appstore.utils.SkyConstants;

public class UICommon {
    public static int tipTextSize = SkyConstants.Dpi(51);
    public static int dlgContentTextSize = SkyConstants.Dpi(28);
    public static float disableAlpha = 0.5f;
    public static final int btnWidth = SkyConstants.Div(195);
    public static final int btnHeight = SkyConstants.Div(83);

    public static void setText(TextView text, String content, boolean hideWhenEmpty) {
        if (content == null || content.trim().length() == 0) {
            text.setText("");
            if (hideWhenEmpty)
                text.setVisibility(View.INVISIBLE);
        } else {
            text.setText(content);
            if (hideWhenEmpty)
                text.setVisibility(View.VISIBLE);
        }
    }

    public static void setText(TextView text, String content) {
        setText(text, content, false);
    }

    public static TextView newTextView(Context context, int textSize, int colorId) {
        TextView text = new TextView(context);
        text.setTextSize(textSize);
        text.setSingleLine();
        SkinManager.setTextColor(text, colorId);
        return text;
    }

    public static Button newButton(Context context, int textSize, int colorId) {
        Button btn = new Button(context);
        btn.setTextSize(textSize);
        btn.setSingleLine();
        SkinManager.setTextColor(btn, colorId);
        return btn;
    }

    public static int getStrIdByAction(AppAction action) {
        int strId = 0;
        switch (action) {
            case ACTION_CANCEL:
                strId = R.string.cancel;
                break;
            case ACTION_UNINSTALL:
                strId = R.string.uninstall;
                break;
            case ACTION_DOWNLOAD:
                strId = R.string.download;
                break;
            case ACTION_INSTALL:
                strId = R.string.install;
                break;
            case ACTION_PAUSE:
                strId = R.string.pause;
                break;
            case ACTION_CONTINUE:
                strId = R.string.continue_download;
                break;
            case ACTION_LAUNCH:
                strId = R.string.launch;
                break;
            case ACTION_UPGRADE:
                strId = R.string.upgrade;
                break;
            case ACTION_DELETE:
                strId = R.string.delete;
                break;
        }
        return strId;
    }

    public static String sizeToString(float size) {
        String sizeStr = "";
        if (size < 102.4) {
            size = (float) (Math.round(size * 100)) / 100;
            if (size < 1) {
                size = 0;
            }
            sizeStr = size + "MB";
        } else {
            size = size / 1024;
            size = (float) (Math.round(size * 100)) / 100;
            sizeStr = size + "GB";
        }
        return sizeStr;
    }
}
